/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.data.bean.base.Niudan;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.adapter.SkuItemAdapter;
import com.simon.mengkou.ui.base.BaseNiudanFragment;
import com.simon.mengkou.ui.view.StrokeTextView;
import com.simon.mengkou.ui.view.slidinglayer.SlidingLayer;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/14.
 * desc    : 扭蛋详情
 */
public class NDDetailFragment extends BaseNiudanFragment {
    //layer
    @Bind(R.id.detail_id_layer)
    SlidingLayer mLayer;
    //扭蛋图片
    @Bind(R.id.detail_id_image) SimpleDraweeView mSdvImage;
    //扭蛋标题
    @Bind(R.id.detail_id_name) TextView mTvName;
    //skus
    @Bind(R.id.detail_id_grid) GridView mGvSkus;
    //扭蛋价格
    @Bind(R.id.detail_id_price)
    StrokeTextView mStvPrice;
    //扭蛋淘宝价格
    @Bind(R.id.detail_id_taobao_price)
    StrokeTextView mStvTaobaoPrice;
    //内容root
    @Bind(R.id.detail_id_root) LinearLayout mLlContentRoot;


    private Niudan mNiudan;


    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        Bundle bundle = getArguments();
        if(bundle != null) {
            mNiudan = (Niudan) bundle.getSerializable(CstOuer.KEY.NIUDAN);
        }
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_detail);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, getView());
        mActivity.setDoorVisible(false);

        mLayer.setStickTo(SlidingLayer.STICK_TO_TOP);
        mLayer.setChangeStateOnTap(false);
        mLayer.setSlidingEnabled(false);

        setNiudanDetail();

        //打开门
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mLayer.setOnInteractListener(new SlidingLayer.OnInteractListener() {
                    @Override
                    public void onOpen() {

                    }

                    @Override
                    public void onShowPreview() {

                    }

                    @Override
                    public void onClose() {

                    }

                    @Override
                    public void onOpened() {
                        getDetail();
                    }

                    @Override
                    public void onPreviewShowed() {

                    }

                    @Override
                    public void onClosed() {

                    }
                });
                mLayer.openLayer(true);
            }
        }, 300);

    }


    /**
     *开始扭蛋
     */
    @OnClick(R.id.detail_id_play)
    public void startPlay() {
        mLayer.setOnInteractListener(new SlidingLayer.OnInteractListener() {
            @Override
            public void onOpen() {

            }

            @Override
            public void onShowPreview() {

            }

            @Override
            public void onClose() {
            }

            @Override
            public void onOpened() {

            }

            @Override
            public void onPreviewShowed() {

            }

            @Override
            public void onClosed() {
                mActivity.getSupportFragmentManager().popBackStack();
                OuerDispatcher.presentPlayFragment(mActivity, mNiudan);
            }
        });

        mLayer.closeLayer(true);
    }

    /**
     * 开始购买
     */
    @OnClick(R.id.detail_id_buy)
    public void startBuy() {
        UtilOuer.toast(mActivity, R.string.common_feature_disable);
    }

    /**
     * 获取扭蛋详情
     */
    private void getDetail() {
        String id = mNiudan == null ? null : mNiudan.getId();
        AgnettyFuture future = OuerApplication.mOuerFuture.getNiudanDetail(id,
                new OuerFutureListener(mActivity){
                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        mNiudan = (Niudan) result.getAttach();
                        setNiudanDetail();
                    }
                });

        attachDestroyFutures(future);
    }

    private void setNiudanDetail() {
        if(mNiudan == null) {
            return;
        }

        mLlContentRoot.setVisibility(View.VISIBLE);
        //扭蛋图片
        Image image = mNiudan.getImage();
        if(image.getHeight() != 0 && image.getWidth() != 0) {
            float ratio = image.getWidth() * 1f / image.getHeight();
            mSdvImage.setAspectRatio(ratio);
        }
        GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources())
                .setFadeDuration(300)
                .setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
        OuerApplication.mImageLoader.loadImage(mSdvImage, mNiudan.getImage().getUrl(),builder);

        //扭蛋标题
        mTvName.setText(mNiudan.getName());

        //扭蛋价格
        String price = UtilOuer.formatPrice(mNiudan.getPrice());
        mStvPrice.setText(getString(R.string.options_price, price));

        //扭蛋淘宝价格
        String taobaoPrice = UtilOuer.formatPrice(mNiudan.getTaobaoPrice());
        mStvTaobaoPrice.setText(getString(R.string.options_taobao_price, taobaoPrice));

        //显示扭蛋sku
        SkuItemAdapter adapter = new SkuItemAdapter(mActivity, mNiudan.getSku());
        mGvSkus.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {
        mLayer.setOnInteractListener(new SlidingLayer.OnInteractListener() {
            @Override
            public void onOpen() {

            }

            @Override
            public void onShowPreview() {

            }

            @Override
            public void onClose() {
            }

            @Override
            public void onOpened() {

            }

            @Override
            public void onPreviewShowed() {

            }

            @Override
            public void onClosed() {
                mActivity.getSupportFragmentManager().popBackStack();
                setDoorVisible(true);
                openDoor(null);
            }
        });

        mLayer.closeLayer(true);

    }

}
